package com.ggh.jinjilive.ui.home.video.videoplay;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.OrientationHelper;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityVideoPlayBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.home.video.VideoDetails;
import com.ggh.jinjilive.mobile.home.video.VideoList;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.home.video.videoplay.adapter.VideoPlayAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.CommonUtils;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.views.video.cache.PreloadManager;
import com.ggh.jinjilive.views.video.controller.TikTokController;
import com.ggh.jinjilive.views.video.render.TikTokRenderViewFactory;
import com.ggh.jinjilive.views.video.tiktok.OnViewPagerListener;
import com.ggh.jinjilive.views.video.tiktok.ViewPagerLayoutManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import xyz.doikki.videoplayer.player.VideoView;

/**
 * 视频播放
 */

@Route(path = ARouterUtils.VideoPlayActivity)
public class VideoPlayActivity extends BaseActivity<ActivityVideoPlayBinding> {
    /**
     * 视频集合
     */
    private List<VideoList> mVideoListList;
    private int mIndex;
    //适配器
    private VideoPlayAdapter videoPlayAdapter;
    /**
     * 当前播放位置
     */
    private int mCurPos;
    //播放器
    private VideoView mVideoView;
    private TikTokController mController;
    private  VideoPlayAdapter.ItemViewHolder viewHolder;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).statusBarDarkFont(false).navigationBarColor(R.color.white).navigationBarDarkIcon(true).init();
    }

    @Override
    public void initParam() {
        super.initParam();
        Intent intent = getIntent();
        mIndex = intent.getIntExtra("txlive_info_position",0);
        String videoListList = intent.getStringExtra("videoListList");
        mVideoListList = new Gson().fromJson(videoListList, new TypeToken<List<VideoList>>() {}.getType());
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_video_play;

    }

    @Override
    public void initView() {
        super.initView();
        mVideoView = new VideoView(this);
        //以下只能二选一，看你的需求
        mVideoView.setRenderViewFactory(TikTokRenderViewFactory.create());
        mVideoView.setLooping(true);
        mController = new TikTokController(this);
        //根据屏幕方向自动进入/退出全屏
        mController.setEnableOrientation(true);
        mVideoView.setVideoController(mController);

    }

    @Override
    public void initData() {
        super.initData();
        videoPlayAdapter = new VideoPlayAdapter(mVideoListList);
        videoPlayAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                VideoList videoList = (VideoList) mObject;
                if(position == 0){//关注
                    if(videoList.getIs_follow() == 0){

                    }
                }else if(position == 1){
                    GeneralUtils.videoTop(videoList.getVideo_id(), new AddOnClickListener1() {
                        @Override
                        public void onSkipClicked(Object mObject) {
                            //获取视频详情
                            videoView(videoList);
                        }
                    });
                }
            }
        });
        ViewPagerLayoutManager layoutManager = new ViewPagerLayoutManager(this, OrientationHelper.VERTICAL);
        binding.vp2Videoplay.setLayoutManager(layoutManager);
        binding.vp2Videoplay.setAdapter(videoPlayAdapter);
        layoutManager.setOnViewPagerListener(new OnViewPagerListener() {
            @Override
            public void onInitComplete() {
                //自动播放第index条
                startPlay(mIndex);
            }

            @Override
            public void onPageRelease(boolean isNext, int position) {
                if (mCurPos == position) {
                    mVideoView.release();
                }
            }

            @Override
            public void onPageSelected(int position, boolean isBottom) {
                if (mCurPos == position){
                    return;
                }
                startPlay(position);
            }
        });
        //
        binding.vp2Videoplay.scrollToPosition(mIndex);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();


    }

    /**
     * 获取视频详情
     * @param videoList
     */
    public void videoView(VideoList videoList){
        //获取视频详情
        GeneralUtils.videoView(videoList.getVideo_id(), new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                VideoDetails videoDetails = (VideoDetails) mObject;
                if(null != videoDetails){
                    //  是否关注
                    if(videoDetails.getIs_follow() == 0){
                        viewHolder.img_attention.setVisibility(View.VISIBLE);
                    }else if(videoDetails.getIs_follow() == 1){
                        viewHolder.img_attention.setVisibility(View.GONE);
                    }
                    // 是否喜欢当前 视频
                    if (videoDetails.getIs_like() == 0) {
                        //  没有喜欢当前视频
                        viewHolder.img_point_praise.setImageResource(R.mipmap.icon_love_video);
                    } else if (videoDetails.getIs_like() == 1) {
                        // 喜欢当前视频
                        viewHolder.img_point_praise.setImageResource(R.mipmap.icon_love_dieo_praise);
                    }
                    //  喜欢人数
                    viewHolder.likeText.setText(String.valueOf(videoDetails.getLike_num()));
                    // 评论次数
                    viewHolder.commentText.setText(String.valueOf(videoDetails.getComm_num()));
                    // 分享次数
                    viewHolder.shareNum.setText(String.valueOf(videoDetails.getShare_num()));

                }
            }
        });
    }

    /**
     * 播放
     * @param position
     */
    private void startPlay(int position){
        View itemView = binding.vp2Videoplay.getChildAt(0);
        viewHolder = (VideoPlayAdapter.ItemViewHolder) itemView.getTag();
        mVideoView.release();
        CommonUtils.removeViewFormParent(mVideoView);
        VideoList videoList = mVideoListList.get(position);
        if(null != videoList){
            //标题与内容
            viewHolder.tv_title.setText(videoList.getTitle());
            viewHolder.tv_nickname.setText("@"+videoList.getNickname());
            //头像
            Glide.with(viewHolder.riv.getContext()).load(videoList.getHead_portrait()).into(viewHolder.riv);
            //关注
            viewHolder.img_attention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            //  是否关注
            if(videoList.getIs_follow() == 0){
                viewHolder.img_attention.setVisibility(View.VISIBLE);
            }else if(videoList.getIs_follow() == 1){
                viewHolder.img_attention.setVisibility(View.GONE);
            }
            //视频点赞
            viewHolder.ll_point_praise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GeneralUtils.videoTop(videoList.getVideo_id(), new AddOnClickListener1() {
                        @Override
                        public void onSkipClicked(Object mObject) {
                            //获取视频详情
                            videoView(videoList);
                        }
                    });
                }
            });

            // 是否喜欢当前 视频
            if (videoList.getIs_like() == 0) {
                //  没有喜欢当前视频
                viewHolder.img_point_praise.setImageResource(R.mipmap.icon_love_video);
            } else if (videoList.getIs_like() == 1) {
                // 喜欢当前视频
                viewHolder.img_point_praise.setImageResource(R.mipmap.icon_love_dieo_praise);
            }
            //  喜欢人数
            viewHolder.likeText.setText(String.valueOf(videoList.getLike_num()));
            // 评论次数
            viewHolder.commentText.setText(String.valueOf(videoList.getComm_num()));
            // 分享次数
            viewHolder.shareNum.setText(String.valueOf(videoList.getShare_num()));

        }

        String playUrl = PreloadManager.getInstance(this).getPlayUrl(videoList.getMovieurls());
        mVideoView.setUrl(playUrl);
        //请点进去看isDissociate的解释
        mController.addControlComponent(viewHolder.mTikTokView, true);
        viewHolder.mPlayerContainer.addView(mVideoView, 0);
        mVideoView.start();
        mCurPos = position;
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (mVideoView != null) {
            mVideoView.resume();
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (mVideoView != null) {
            mVideoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVideoView != null) {
            mVideoView.release();
        }
    }

    @Override
    public void onBackPressed() {
        if (mVideoView == null || !mVideoView.onBackPressed()) {
            super.onBackPressed();
        }
    }
}
